<?php

namespace Admin\Controller;

class MatchController extends BaseManageController {


	protected $modelName = 'Admin/Match';		// 必须定义
	protected $is_del 	 = false; 		// 是否真实删除
	protected $text_map = array(
		'status' => array( 0 => '禁用', '1' => '开启', '-1' => '删除'),
		'match_state' => array( 0 => '普通', 1=>'预告', 2 => '直播中'),
	);		// 字符和文字的映射关系

	public function _before_index()
	{	
		$search = array();
		$this->game = $game = I('game', 0, 'intval');
        if($game){
       		$search['game_id'] = $game;
        }

		$title = I('title');
		if($title)
			$search['title'] = array('like', (string)$title);

		$this->where = $search;
		
		$this->gameInfo(true);

	}

	public function _before_add()
	{
		//$this->setGame();
		$this->gameInfo();
	}

	public function _before_edit()
	{
		//$this->setGame();
		$this->gameInfo();
	}

	protected function setGame()
	{
		if (!I('game_id'))
		{
			$this->error('游戏信息错误');
		}
		$this->game_id = I('game_id', 0, 'intval');
		$this->game    = A('Api/Game', 'Event')->info($this->game_id);
	}

	/**
	*游戏信息获取
	*/
	private function gameInfo($format=false){

		$games = A('Api/Game', 'Event')->lists(array(), 'id,name');

		if($format===false){

			$this->games = $games;

		}else{

			$temp = array();
			foreach ($games as $key => $value) {
				$temp[$value['id']] = $value['name'];
			}

			$this->games = $temp;
		}
	}

	protected function setIcons()
	{
		$this->iconsOptions = A('Api/IconPacks', 'Event')->getIcons(2);
	}


	

	public function sync()
	{
		// 调整数据结构脚本
		 

	}
	


	/** 
	 * 修改赛事的赛程状态
	 * @author Legend. <xcx_legender@qq.com>
	 * @param 
	 * @return 
	 */
	
	public function changeState($id, $state = 0)
	{
		$map['id'] = intval($id);
		$this->model->where($map)->setField('match_state', intval($state));
		$this->success('更改赛程状态成功');
	}

	/**
	*删除赛事的前置操作
	*有比赛的，则不能删除
	*@author susy.liang<liangling@ilongyuan.com.cn>
	*/
	public function _before_del(){
		$ids = array_unique((array)I('id',0));
		$ids = array_map('intval', $ids);

		$rgService = D('Common/RaceGroup', 'Service');
		$map = array(
				'match_id'=>array('in', $ids),
				'status'=>array('egt', $rgService::STATUS_PEDDING)
			);
		$raceGrp = $rgService->where($map)->select();
		if($raceGrp)
			$this->error('赛事下有比赛，请先删除比赛');
	}
}